package com.apache.common;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import com.apache.tools.DateUtils;
/**
 * 时间处理帮助类
 * @author lvcai 
 */
public class DateUtil extends DateUtils {
	//用来全局控制 上一周，本周，下一周的周数变化    
    private static int weeks = 0;    
	/**
	 * 获取当前日期时间，格式yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getNowTime(){
		return format(new Date(), DateUtils.FmtStr.yyyyMMdd_HHmmss.toString());
	}
	
	/**
	 * 获取当前日期时间，格式yyyy-MM-dd
	 * @return
	 */
	public static String getNowTimeWithOutMinutes(){
		return format(new Date(), DateUtils.FmtStr.yyyyMMdd.toString());
	}
	/**
	 * 拼装日期 格式：yyyy-MM-dd 
	 * @param year 年份
	 * @param month 月份
	 * @param day 日
	 * @return
	 */
	private static String getDateStr(int year,int month,int day){
		String monthStr=String.valueOf(month);
		String dayStr=String.valueOf(day);
		if (month<10) {
			monthStr="0"+month;
		}
		if (day<10) {
			dayStr="0"+day;
		}
		return year+"-"+monthStr+"-"+dayStr;
	}
	/**
	 * 获取指定日期的前一天日期
	 * @param date
	 * @return
	 */
	public static String getLastDate(Date date){
		Calendar cd = Calendar.getInstance();
		cd.setTime(date);
		cd.add(GregorianCalendar.DATE, -1);    
		return dateToStr(cd.getTime());
	}
	/**
	 * 获取指定日期的后一天日期
	 * @param date
	 * @return
	 */
	public static String getNextDate(Date date){
		Calendar cd = Calendar.getInstance();
		cd.setTime(date);
		cd.add(GregorianCalendar.DATE, 1);    
		return dateToStr(cd.getTime());
	}
	/**   
	 * 将时间转换为短时间格式字符串 yyyy-MM-dd    
	 * @param strDate   
	 * @return   
	 */   
	 private static String dateToStr(Date date) {    
	     SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
	     return formatter.format(date);    
	 }
	 /**   
		 * 格式化日期为短时间格式字符串 yyyy-MM-dd    
		 * @param date   
		 * @return   
	     * @throws ParseException 
		 */ 
	 public static String formateDate(String date) throws ParseException{
		 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
		 Date d=formatter.parse(date);
	     return formatter.format(d);  
	 }
	 /**
	  * 获得某日期与周日相差的天数    
	  * @param date
	  * @return
	  */
	 private static int getMondayPlus(Date date) {    
	     Calendar cd = Calendar.getInstance();
	     cd.setTime(date);
	     // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......    
	     int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK)-1;         //因为按中国礼拜一作为第一天所以这里减1  
	     if (dayOfWeek == 1) {    
	         return 0;    
	     } else {    
	         return 1 - dayOfWeek;    
	     }    
	 }  
	/**
	 * 获得指定日期对应本周星期日的日期           
	 * @param date
	 * @return
	 */
	 public static String getCurrentWeekday(Date date) {    
	     weeks = 0;    
	     int mondayPlus = getMondayPlus(date);    
	     GregorianCalendar currentDate = new GregorianCalendar(); 
	     currentDate.setTime(date);
	     currentDate.add(GregorianCalendar.DATE, mondayPlus+6);    
	     Date monday = currentDate.getTime();    
	     String preMonday = dateToStr(monday);    
	     return preMonday;    
	 }    
	     
	/**
	 *  获得指定日期对应本周星期一的日期     
	 * @param date
	 * @return
	 */
	 public static String getMondayOFWeek(Date date){    
	      weeks = 0;    
	      int mondayPlus = getMondayPlus(date);    
	      GregorianCalendar currentDate = new GregorianCalendar(); 
	      currentDate.setTime(date);
	      currentDate.add(GregorianCalendar.DATE, mondayPlus);    
	      Date monday = currentDate.getTime();    
	      String preMonday = dateToStr(monday); 
	      return preMonday;    
	 }    
	  
	     
	/**
	 *  获得指定日期对应上周星期日的日期    
	 * @param date
	 * @return
	 */
	 public static String getPreviousWeekSunday(Date date) {    
	     weeks=0;    
	     weeks--; 
	     int mondayPlus = getMondayPlus(date);    
	     GregorianCalendar currentDate = new GregorianCalendar(); 
	     currentDate.setTime(date);
	     currentDate.add(GregorianCalendar.DATE, mondayPlus+weeks);    
	     Date monday = currentDate.getTime();    
	     DateFormat df = DateFormat.getDateInstance();    
	     String preMonday = dateToStr(monday);    
	     return preMonday;    
	 }    

	/**
	 *  获得指定日期对应上周星期一的日期    
	 * @param date
	 * @return
	 */
	 public static String getPreviousWeekday(Date date) {
		 weeks=0;
	     weeks--;    
	     int mondayPlus = getMondayPlus(date);    
	     GregorianCalendar currentDate = new GregorianCalendar();    
	     currentDate.setTime(date);
	     currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks);    
	     Date monday = currentDate.getTime();    
	     DateFormat df = DateFormat.getDateInstance();    
	     String preMonday = dateToStr(monday);    
	     return preMonday;    
	 }    
	 /**
	  *  获得指定日期对应周星期一的日期       
	  * @param date
	  * @return
	  */
	 public static String getNextMonday(Date date) {    
	     weeks++;    
	     int mondayPlus = getMondayPlus(date);    
	     GregorianCalendar currentDate = new GregorianCalendar();   
	     currentDate.setTime(date);
	     currentDate.add(GregorianCalendar.DATE, mondayPlus + 7);    
	     Date monday = currentDate.getTime();    
	     DateFormat df = DateFormat.getDateInstance();    
	     String preMonday = dateToStr(monday);     
	     return preMonday;    
	 }    
	     
	/**
	 * 获得指定日期对应下周星期日的日期    
	 * @param date
	 * @return
	 */
	 public static String getNextSunday(Date date) {
		 weeks++;
	     int mondayPlus = getMondayPlus(date);    
	     GregorianCalendar currentDate = new GregorianCalendar();    
	     currentDate.setTime(date);
	     currentDate.add(GregorianCalendar.DATE, mondayPlus + 7+6);    
	     Date monday = currentDate.getTime();    
	     DateFormat df = DateFormat.getDateInstance();    
	     String preMonday = dateToStr(monday);    
	     return preMonday;    
	 }
	/**
	 * 获取某日期满足类型的 1.前阶段开始结束日期 2.本阶段开始结束日期。格式：yyyy-MM-dd
	 * @param type 1=按日，2=按周，3=按月，4=按年
	 * @param dateStr 指定的日期字符串 非空
	 * @return 根据type不同返回不同的数组如：参数=[2,2012-07-18] --> 返回[2012-07-09,2012-07-15,2012-07-16,2012-07-22]
	 */
	public static String[] getDateByType(int type,String dateStr){
		String pattern="yyyy-MM-dd";
		String lastDateBegin=dateStr;
		String lastDateEnd=dateStr;
		String nowDateBegin=dateStr;
		String nowDateEnd=dateStr;
		try {
			Date date = parse(dateStr);
			switch (type) {
			case 1:
				//1.按日查询
				nowDateBegin=format(date, pattern);
				nowDateEnd=nowDateBegin;
				lastDateBegin=format(beforeNDays(date,1), pattern);
				lastDateEnd = lastDateBegin;	
				break;
			case 2:
				//2.按周查询
				nowDateBegin = getMondayOFWeek(date);
				nowDateEnd = getCurrentWeekday(date);
				lastDateBegin = getPreviousWeekday(date);    
				lastDateEnd = getPreviousWeekSunday(date);
				break;
			case 3:
				//2.按月查询
				nowDateBegin=getDateStr(getYear(date),getMonth(date)+1,1);
				nowDateEnd=getDateStr(getYear(date),getMonth(date)+1,getLastDayOfTheMonth(date));
				lastDateBegin=getDateStr(getYear(date),getMonth(date),1);
				Date lastDate=parse(lastDateBegin);
				lastDateEnd = getDateStr(getYear(date),getMonth(lastDate)+1,getLastDayOfTheMonth(lastDate));
				break;
			case 4:
				//2.按年查询
				nowDateBegin=getDateStr(getYear(date),1,1);
				nowDateEnd=getDateStr(getYear(date),12,31);
				lastDateBegin = getDateStr(getYear(date)-1,1,1);
				lastDateEnd = getDateStr(getYear(date)-1,12,31);
				break;
			}
			//System.out.println(type+" (日周月年).   "+lastDateBegin+" 至   "+lastDateEnd+" |  "+nowDateBegin+"  至   "+nowDateEnd);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new String[]{lastDateBegin,lastDateEnd,nowDateBegin,nowDateEnd};
	}
	
	/*
	 * 为了适应银行日期格式，截取时间信息
	 */
	public static String getTimeInfo(String time,String op){
		String info=null;
		if(time==null||"".equals(time)||"".equals(op)){
			info="";
		}
		
		if("date".equals(op.trim())){
			time=time.substring(0, 10);
			info=time.replaceAll("-","");
		}else if("time".equals(op.trim())){
			time=time.substring(11, 19);
			info=time.replaceAll(":","");
		}
		return info;
	}
	
	/*
	 * 处理银行返回的时间，转换为正常的时间日期
	 */
	public static String formateBankTime(String date,String op){
		String time="";
		if(date!=null&&(date.length()==8||date.length()==6)){
			if("date".equals(op.trim())){
				time=date.substring(0, 4)+"-"+date.substring(4, 6)+"-"+date.substring(6, 8);
			}else if("time".equals(op.trim())){
				time=date.substring(0, 2)+":"+date.substring(2, 4)+":"+date.substring(4, 6);
			}
		}
		return time;
	}
	
	/*
	 * 合并银行返回的字符串为 带年月日 时分秒的时间戳
	 */
	public static String mergeDateStamp(String date,String time){
		return date+" "+time;
	}
	
	/*
     * 吕才  获取两日期的天数差
     */
    public static long getBetweenDays(String startDay,String endDay){
    	DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
		Long sdate = null; 
		Long edate = null;
		try { 
			sdate = df.parse(startDay).getTime();
			edate = df.parse(endDay).getTime();
		} catch (Exception e) { 
			System.out.println("时间转换有误！"); 
		}
		
		long num=edate-sdate;
		long oneDaylong = 24*60*60*1000; 
		
		return num/oneDaylong;
    }
    
    public static Date getTheBeforeXDate(Date time,int days){
		 Calendar c = Calendar.getInstance();
	     Date date = null;

	     c.setTime(time);
	     int day = c.get(Calendar.DATE);
	     c.set(Calendar.DATE, day + days);
	     
	     return c.getTime();
	}

	
	public static void main(String[] args) {
		System.out.println("2012-08-29 12:20:10".length());
		System.out.println(DateUtil.getTimeInfo("2012-08-29 12:20:10", "date"));
		System.out.println(DateUtil.getTimeInfo("2012-08-29 12:20:10", "time"));
	}
}
